/**
 * KopsoxFramework
 * Copyright (C) 2010 Free Software Foundation, Inc. <http://fsf.org/>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kopsox.util.xml.parser.impl;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.kopsox.util.xml.parser.KopsoxXMLParser;
import org.kopsox.util.xml.parser.exception.KopsoxXMLParserException;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * This class implements the KopsoxXMLParser and the SAX-DefaultHandler.<br>
 * 
 * @see org.xml.sax.helpers.DefaultHandler
 * @see org.kopsox.util.xml.parser.KopsoxXMLParser
 * @author Konrad Renner - 23.07.2010
 */
public abstract class AbstractKopsoxXMLParser extends DefaultHandler implements KopsoxXMLParser {

	private String xmlFile;
	
	public AbstractKopsoxXMLParser(String xmlFile) {
		this.xmlFile = xmlFile;
	}
	
	protected InputStream createXMLStreamReader()  throws KopsoxXMLParserException {
		InputStream stream = this.getClass().getClassLoader().getResourceAsStream(xmlFile);
		
		if(stream == null) {
			throw new KopsoxXMLParserException(new FileNotFoundException("Cannot load path with the Classloader:"+xmlFile));
		}
		return stream;
	}
	
	@Override
	public void parseXML() throws KopsoxXMLParserException {
		//Loading from the Classpath
		InputStream stream = createXMLStreamReader();
		
		SAXParserFactory factory = SAXParserFactory.newInstance(); 
		try {
			SAXParser saxParser = factory.newSAXParser();
			
			saxParser.parse( stream, this );
			
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
			throw new KopsoxXMLParserException(e);
		} catch (SAXException e) {
			e.printStackTrace();
			throw new KopsoxXMLParserException(e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new KopsoxXMLParserException(e);
		}
	}
	
	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#resolveEntity(java.lang.String, java.lang.String)
	 */
	@Override
	public InputSource resolveEntity(String arg0, String arg1)throws IOException, SAXException {
		InputStream stream = this.getClass().getClassLoader().getResourceAsStream(getPathToDTD());
		
		return new InputSource( new InputStreamReader( stream ) );
	}
	
	
	/**
	 * Gets the path to the DTD of the XML-File
	 * 
	 * @return String
	 */
	abstract public String getPathToDTD();
	
	abstract public void startElement( String namespaceURI, String localName, String qName, Attributes atts ) throws SAXException;
}
